package com.utc.cards.common.view.listener;

import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;

import com.utc.cards.common.view.CardsContainer;
import com.utc.cards.model.card.Card;

public class CardDragListener implements OnDragListener
{
    private CardsContainer _container;

    private View _selectedView;
    private float _x;

    public CardDragListener(CardsContainer container)
    {
        _container = container;
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent)
    {
        int action = dragEvent.getAction();

        switch (action)
        {
        case DragEvent.ACTION_DRAG_STARTED:
            SendDragListener.MESSAGE_DEJA_AFFICHE = false;
            _selectedView = (View) dragEvent.getLocalState();
            break;

        case DragEvent.ACTION_DRAG_ENTERED:
            // view.setBackgroundColor(Color.RED);
            break;

        case DragEvent.ACTION_DRAG_EXITED:
            break;

        case DragEvent.ACTION_DRAG_LOCATION:
            if (_selectedView != null)
            {
                _x = dragEvent.getX();

                Card card = (Card) _selectedView.getTag();
                String cardName = card.getName();

                _container.update(cardName, _x);
            }

            break;

        case DragEvent.ACTION_DROP:
            if (_selectedView != null)
            {
                Card card = (Card) _selectedView.getTag();
                String cardName = card.getName();

                _container.update(cardName, _x);

                _selectedView.setVisibility(View.VISIBLE);
            }

            break;

        case DragEvent.ACTION_DRAG_ENDED:
            // view.setBackgroundColor(Color.GREEN);
            if (_selectedView != null)
            {
                System.out.println("X = " + _x);

                Card card = (Card) _selectedView.getTag();
                String cardName = card.getName();

                _container.update(cardName, _x);

                _selectedView.setVisibility(View.VISIBLE);
            }
            _container.refresh();

        default:
            break;
        }

        return true;
    }

}
